package org.aceor.mddal.common.utils;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import java.sql.Date;
import java.sql.Timestamp;

/**
 * Created by lxue on 16/4/12.
 */
public class DateUtil {

    public static long getTime(String date) {
        //使用Timestamp和Date匹配
        try {
            if (isTypeOfTimestamp(date)) {
                return Timestamp.valueOf(date).getTime();
            } else {
                return Date.valueOf(date).getTime();
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("日期格式不支持.仅支持yyyy-MM-DD HH:mm:ss,yyyy-MM-DD两种");
        }
    }

    public static boolean isTypeOfTimestamp(String date) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(date), "入参不能为空");
        return date.contains(" ");
    }
}
